// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

var ClickManager = require('ClickManager')
var NetworkManager = require('NetworkManager')
var UserManager = require('UserManager')

cc.Class({
    extends: cc.Component,

    properties: {
        tableId: 0,
        config1: cc.Label,
        config2: cc.Label,
        current: cc.Label,
        statusText: cc.Label,

        join: cc.Node,
        alertModel: cc.Prefab
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.pnode = cc.find('Canvas')
        this.tableScene = false
        this.linkStatus = false
        var count = 0

        // 加入房间
        ClickManager.onClick(this.join, this, () => {
            if (!this.linkStatus) {
                this.linkStatus = true
                this.linkTableServer()
            }
        })
    },

    // 加入牌桌
    joinTable () {
        var token = UserManager.token
        ClickManager.loading(this.pnode, '加入游戏牌桌中..')
        NetworkManager.onConnectedToGame((gameService) => {
            gameService.proxy.joinGameTable(token, this.tableId, (data) => {
                cc.log('加入游戏牌桌:', data)
                this.linkStatus = false
                if ((data.ok && data.code === 200) || data.code === 201) {
                    UserManager.tid = data.tid

                    ClickManager.loading(this.pnode, '加入游戏牌桌成功...')
                    cc.director.preloadScene('table',  this.onProgress.bind(this), () => {
                        if (!this.tableScene) {
                            cc.director.loadScene('table')
                            ClickManager.rmLoading(this.pnode)
                            this.tableScene = true
                        }
                    })
                } else if (data.ok && data.code === 0) {
                    ClickManager.rmLoading(this.pnode)
                    ClickManager.alertModel(this.alertModel, data.msg)
                } else if (!data.ok) {
                    ClickManager.rmLoading(this.pnode)
                    NetworkManager.onCloseFormGame()
                }
            })
        })
    },

    // 连接牌桌服务器
    linkTableServer () {
        ClickManager.loading(this.pnode, '连接游戏服务器中...')
        if (!NetworkManager.gGameServerIsLogin) {
            NetworkManager.connectAndAuthToGame()
        }
        NetworkManager.onConnectedToGame(() => {
            var loginData = UserManager.loginToGameData
            cc.log('连接游戏服务器:', loginData)
            if (loginData.isInGame) {
                ClickManager.loading(this.pnode, '连接游戏服务器成功...')
                this.joinTable()
            } else {
                // 不在游戏服务器，登录失败
                NetworkManager.clearGameService()
                ClickManager.rmLoading(this.pnode)
                ClickManager.alertModel(this.alertModel, '连接游戏服务器失败')
            }
        })
    },

    // 加载进度
    onProgress (com, total, item) {
        if (total < 10) {
            total = this.initTotal
        }

        let progress = com / total
        progress = isNaN(progress) ? 0 : progress
        ClickManager.loading(this.pnode, '游戏场景加载中('+ (progress*100).toFixed(2) +'%)...')
    },

    // 初始化
    init (data) {
        this.tableId  = data.tableId
        this.join.tid = data.tableId
        this.config1.string = data.config1
        this.config2.string = data.config2
        this.current.string = data.current
        this.statusText.string = data.statusText
    }

    // update (dt) {},
});
